using UnityEngine;
using System.Collections;

public class UtilsMesh {
	
	public static Vector2 [] rotateUVs(Vector2[] uvs, float angle){
		float xMin = float.MaxValue;
		float yMin = float.MaxValue;
		float xMax = float.MinValue;
		float yMax = float.MinValue;
		
		int i=0, l=uvs.Length;
		Vector2 uv = new Vector2();
		Vector2 [] results = new Vector2[l];
		for(; i<l; ++i){
			uv = uvs[i];
			if(uv.x<xMin) xMin = uv.x;
			if(uv.y<yMin) yMin = uv.y;
			if(uv.x>xMax) xMax = uv.x;
			if(uv.y>yMax) yMax = uv.y;
		}
		
		Vector2 o = new Vector2(xMin+((xMax-xMin)*0.5f),yMin+((yMax-yMin)*0.5f));
		i=0;
		for(; i<l; ++i){
			results[i] = UtilsMath.rotatePoint(uvs[i],angle,o);	
		}
		
		return results;
	}
	
	public static Mesh createPlane(Rect bounds){
		Mesh mesh = new Mesh();
		mesh.vertices = new Vector3[]{new Vector3(bounds.xMin,bounds.yMin,0), new Vector3(bounds.xMax,bounds.yMin,0), new Vector3(bounds.xMin,bounds.yMax,0), new Vector3(bounds.xMax,bounds.yMax,0)};
		mesh.uv = new Vector2[]{new Vector2(0,0), new Vector2(1,0), new Vector2(0,1), new Vector2(1,1)};
		mesh.triangles = new int[]{0,2,1,1,2,3};
		mesh.RecalculateNormals();	
		
		return mesh;
	}
	public static Mesh createPlane(float width, float height){
		float mx = width/2;
		float my = height/2;
		
		Mesh mesh = new Mesh();
		mesh.vertices = new Vector3[]{new Vector3(-mx,-my,0), new Vector3(mx,-my,0), new Vector3(-mx,my,0), new Vector3(mx,my,0)};
		mesh.uv = new Vector2[]{new Vector2(0,0), new Vector2(1,0), new Vector2(0,1), new Vector2(1,1)};
		mesh.triangles = new int[]{0,2,1,1,2,3};
		mesh.RecalculateNormals();	
		
		return mesh;
	}
	
}

